package com.rainng.coursesystem.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class FileStorageService {

    @Value("${file.upload-dir}")
    private String uploadDir;

    public String storeFile(MultipartFile file) throws IOException {
        // 生成一个目标路径
        Path copyLocation = Paths
                .get(uploadDir + "/" + file.getOriginalFilename());

        // 保存文件到目标路径
        Files.copy(file.getInputStream(), copyLocation);

        return copyLocation.toString();
    }
}